package com.guodi.common.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.guodi.common.constant.StatusCode;

import java.io.Serializable;

public class AjaxResult<T> implements Serializable {
    private static final long serialVersionUID = 5990701580664511688L;
    private boolean success = true;
    private Integer ackCode;
    private Integer code;
    private String message;
    private String detailMsg;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public AjaxResult(T data) {
        this.ackCode = 200;
        this.code = 200;
        this.success = true;
        this.data = data;
    }

    public AjaxResult() {
        this.ackCode = 200;
        this.code = 200;
        this.success = true;
    }

    public AjaxResult(Integer errorCode) {
        this.ackCode = errorCode;
        this.code = errorCode;
        this.message = (String) StatusCode.REQUEST_MSG_MAP.get(errorCode);
        this.success = false;
    }

    public AjaxResult(Integer errorCode, String errMsg) {
        this.ackCode = errorCode;
        this.code = errorCode;
        this.message = errMsg;
        this.success = false;
    }

    public AjaxResult(Integer errorCode, String errMsg, T data) {
        this.ackCode = errorCode;
        this.code = errorCode;
        this.message = errMsg;
        this.success = false;
        this.data = data;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getAckCode() {
        return this.ackCode;
    }

    public Integer getCode() {
        return this.code;
    }

    public void setErrorCode(int errCode) {
        this.ackCode = errCode;
        this.code = errCode;
        this.success = false;
        this.message = (String) StatusCode.REQUEST_MSG_MAP.get(errCode);
    }

    public void setError(int errCode, String message) {
        this.ackCode = errCode;
        this.code = errCode;
        this.success = false;
        this.message = message;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public String getDetailMsg() {
        return this.detailMsg;
    }

    public void setDetailMsg(String detailMsg) {
        this.detailMsg = detailMsg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * @deprecated
     */
    @Deprecated
    public void setSuccess(boolean success) {
        if (success) {
            this.ackCode = 1;
            this.code = 1;
        } else {
            this.ackCode = 0;
            this.code = 0;
        }

        this.success = success;
    }
}

